package cn.clx.org.service.impl;

import cn.clx.basic.util.EmailUtil;
import cn.clx.org.domain.Employee;
import cn.clx.org.domain.Shop;
import cn.clx.org.domain.ShopAuditLog;
import cn.clx.org.mapper.EmployeeMapper;
import cn.clx.org.mapper.ShopAuditLogMapper;
import cn.clx.org.mapper.ShopMapper;
import cn.clx.org.service.IShopAuditLogService;
import cn.clx.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author clx
 * @since 2022-06-13
 */
@Service
public class ShopAuditLogServiceImpl extends BaseServiceImpl<ShopAuditLog> implements IShopAuditLogService {

    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private JavaMailSender javaMailSender;



    /**
     * 店铺入驻审核通过
     * @param shopAuditLog
     */
    @Override
    public void examinationPassed(ShopAuditLog shopAuditLog) {
        //记录日志，操作人是谁
        shopAuditLog.setAuditId(327l);
        //更改状态 店铺状态为待激活
        Shop shop = shopMapper.loadById(shopAuditLog.getShopId());
        shop.setState(1);
        shopMapper.update(shop);
        shopAuditLogMapper.save(shopAuditLog);
        //发送邮件给店长邮箱

        //创建复杂邮件对象
        Employee employee = employeeMapper.loadByShopId(shopAuditLog.getShopId());
        // 第一个参数是要发给的人的邮箱  第二个参数是店铺的id
        EmailUtil.sendMessage("1498467577@qq.com",shopAuditLog.getShopId());

    }

    /**
     * 审核驳回
     * @param shopAuditLog
     */
    @Override
    public void reject(ShopAuditLog shopAuditLog) {
        //设置日志
        shopAuditLog.setAuditId(327l);
        Shop shop = shopMapper.loadById(shopAuditLog.getShopId());
        //设置状态为驳回
        shop.setState(-1);
        shopMapper.update(shop);
        //保存操作日志
        shopAuditLogMapper.save(shopAuditLog);

        //发送邮件去叫店长修改
        Employee employee = employeeMapper.loadByShopId(shopAuditLog.getShopId());
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("1498467577@qq.com");
        message.setTo("1910745620@qq.com");
        message.setSubject("宠物乐园-店铺入驻驳回任务");
        message.setText("亲，您的店铺信息有误，审核不通过，请尽快修改并重新提交审核！！修改链接：" +
                "http://localhost:8081/#/register2?shopId="+shopAuditLog.getShopId());
        javaMailSender.send(message);

    }

}
